<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Released under the MIT License.
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------

namespace app\app\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use app\admin\model\AdminMenuModel;
use think\Validate;
use think\facade\Request;
use api\wxapp\model\Config;
class ConfigController extends AdminBaseController
{
    //处理富文本中的img标签
    private function doimg($str)
    {
        preg_match_all("/<\s*img\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i",$str,$img_array);
        $newstr = $str;
        if($img_array[0]){
            foreach($img_array[2] as $k=>$v){
                $newstr = str_replace($v,config()['app']['url'].$v,$newstr);
            }
        }
        return $newstr;
    }
    //checkimg
    private function checkimg($str)
    {
        preg_match_all("/<\s*img\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i",$str,$img_array);
        $newstr = $str;
        if($img_array[0]){
            foreach($img_array[2] as $k=>$v){
                //判断图片是否有http
                if(!strstr($v,'http')){
                    $newstr = str_replace($v,config()['app']['url'].$v,$newstr);
                }
            }
        }
        return $newstr;
    }


    //关于我们
    public function about()
    {
        if($_POST){
            $validate = new Validate([
                'name'  => 'require',
                'date' => 'require',
                'history' => 'require',
                'address' => 'require',
                'logo'=>'require',
                'phone'=>'require',
            ]);
            if(!$validate->check($_POST)){
                $this->error($validate->getError());
            }
            $_POST['addtime'] = date('Y-m-d H:i:s',time());
            if(!$_POST['id']){
                if(!$_POST['img']){
                    $this->error('请上传图片!!!');
                }
                $_POST['img'] = '/upload/'.$_POST['img'];
                $_POST['content'] = $this->doimg($_POST['content']);
                $res = DB::name('app_about')->insert($_POST);
            }else{
                if($_POST['img']){
                    //查询图片手修改
                    $data['img'] = '/upload/'.$_POST['img'];
                }
                $data['id'] = $_POST['id'];
                $data['name'] = $_POST['name'];
                $data['date'] = $_POST['date'];
                $data['history'] = $this->checkimg($_POST['history']);
                $data['address'] = $_POST['address'];
                $data['logo'] = $_POST['logo'];
                $data['phone'] = $_POST['phone'];
                $res = DB::name('app_about')->update($data);
            }

            if($res){
                $this->success('成功');
            }else{
                $this->error('失败');
            }
        }
        $info = db::name('app_about')->find();
        $this->assign('info',$info);
        $this->assign('logo',json_encode($info['logo']));
        return $this->fetch();
    }

    //上传图片
    public function upload(){
        $file = request()->file('file');
        $path = 'upload/admin/';
        $info = $file->validate(['size'=>156780000,'ext'=>'jpg,png,gif'])->move($path);
        if($info) {
            $infopath=str_replace("\\","/",$info->getSaveName());
            return json(['code'=>200,'msg'=>'上传成功','data'=>'/upload/admin/'.$infopath]);
        }else{
            return json(['code'=>400,'msg'=>'上传失败','data'=>$file->getError()]);
        }
    }

    //常量配置
    public function lst()
    {
        $where[] = ['id','NOTIN',[8,9]];
        $list = Config::where($where)->all();
        $this->assign('list',$list);
        return $this->fetch();
    }
    //常量编辑
    public function edit()
    {
        $params = $this->param();
        if($_POST){
            $validate = new Validate([
                'name'  => 'require',
                'value' => 'require',
            ]);
            if(!$validate->check($_POST)){
                $this->error($validate->getError());
            }
            $res = Config::update($_POST);
            if($res){
                $this->success('成功');
            }else{
                $this->error('失败');
            }
        }
        $info = Config::get($params['id']);
        $this->assign('info',$info);
        return $this->fetch();
    }
    //皮肤测试规则
    public function test()
    {
        if($_POST){
            if(!isset($_POST['value'])){
                $this->error('规则内容不能为空!!!');
            }
            $res = Config::update($_POST);
            if($res){
                $this->success('成功');
            }else{
                $this->error('失败');
            }
        }
        $info = Config::get(8);
        $this->assign('info',$info);
        return $this->fetch();
    }
    //护肤常识
    public function sense()
    {
        if($_POST){
            if(!isset($_POST['value'])){
                $this->error('规则内容不能为空!!!');
            }
            $res = Config::update($_POST);
            if($res){
                $this->success('成功');
            }else{
                $this->error('失败');
            }
        }
        $info = Config::get(9);
        $this->assign('info',$info);
        return $this->fetch();
    }
}
